package com.tianqu.tms.www.rental.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.RentalLocationInfo;
import com.tianqu.tms.www.rental.action.RentalLocationListAction;
import com.tianqu.tms.www.rental.actionform.RentalContractAddInputForm;
import com.tianqu.tms.www.rental.actionform.RentalLocationAddInputForm;
import com.tianqu.tms.www.car.actionform.CarGatesInfoIOInputForm;
import com.tianqu.tms.www.rental.viewhelper.RentalLocationViewHelper;;

public class RentalLocationListController extends TmsController {
	private Logger logger = Logger.getLogger(RentalLocationListController.class);
	public static final String PAGING = "paging";

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		//添加事务提交异常捕获
		try {
			controller.begin();
			
			//从前台传过来一个form表单
			RentalLocationAddInputForm rentalForm = (RentalLocationAddInputForm) uc.getInputForm();
			
			RentalLocationInfo rentalLocationInfo = RentalLocationInfo.getInstance();
			
			 if(uc.getAttribute(RentalLocationListAction.PAGENO) != null) {
				 rentalLocationInfo.setPageNo(Integer.parseInt((String)uc.getAttribute(RentalLocationListAction.PAGENO)));
			}
			if(uc.getAttribute(RentalLocationListAction.MAXINDEX) != null) {
				rentalLocationInfo.setMaxIndex(Integer.parseInt((String)uc.getAttribute(RentalLocationListAction.MAXINDEX)));
			}
			if(uc.getAttribute(RentalLocationListAction.SORTKEY) != null) {
				rentalLocationInfo.setSortKey(Integer.parseInt((String)uc.getAttribute(RentalLocationListAction.SORTKEY)));
			}
			if(uc.getAttribute(RentalLocationListAction.ASC) != null) {
				rentalLocationInfo.setAsc((new Boolean((String)uc.getAttribute(RentalLocationListAction.ASC))).booleanValue());
			}

			// 判定翻页
			if (uc.getAttribute("seniorForm") != null) {
				rentalForm = (RentalLocationAddInputForm) uc.getAttribute("seniorForm");
			}
				
			List<RentalLocationInfo> list = new ArrayList<RentalLocationInfo>();
			list = rentalLocationInfo.findRentalLocationList(rentalForm.getLocationSerial(),rentalForm.getStrRentalType(),rentalForm.getRentalStatus(),
					rentalForm.getAcreageS(),rentalForm.getAcreageE(),rentalForm.getCarPortS(),rentalForm.getCarPortE(),rentalForm.getRentalArea(),
					rentalForm.getUpdateTimeS(),rentalForm.getUpdateTimeE());
			
			Codes code = Codes.getInstance();
			
			List<RentalLocationViewHelper> rentalLocationViewHelperlist = new ArrayList<RentalLocationViewHelper>();
			
			if (null != list && list.size() >0) {
				
				for (int i = 0; i <list.size(); i++) {
					RentalLocationInfo locationInfo = new RentalLocationInfo();
					RentalLocationViewHelper rentalLocationViewHelper = new RentalLocationViewHelper();
					locationInfo = list.get(i);
					rentalLocationViewHelper.setId(locationInfo.getId());
					rentalLocationViewHelper.setRowNo(locationInfo.getRowNo());
					rentalLocationViewHelper.setLocationSerial(locationInfo.getLocationSerial());
					rentalLocationViewHelper.setRentalTypeName(locationInfo.getRentalTypeName());
					rentalLocationViewHelper.setAcreage(StringUtil.getPointBetweenThree(locationInfo.getAcreage()));
					rentalLocationViewHelper.setCarPort(StringUtil.getPointBetweenThree(locationInfo.getCarPort()));
					rentalLocationViewHelper.setAddress(locationInfo.getAddress());
					rentalLocationViewHelper.setRentalStatusName(locationInfo.getRentalStatusName());
					rentalLocationViewHelper.setCssFlag(locationInfo.getCssFlag());
					rentalLocationViewHelper.setRentalArea(locationInfo.getArea());
					rentalLocationViewHelper.setRentalAreaName(code.getItemValueByItemIdAndItemCode(Codes.RENTAL_AREA,locationInfo.getArea()));
					rentalLocationViewHelperlist.add(rentalLocationViewHelper);
				}
			}
			uc.setAttribute(TmsConstants.VIEWHELP, rentalLocationViewHelperlist);
			uc.setAttribute(PAGING, rentalLocationInfo);

			logger.debug(this.getClass().getName() + "#execute was completed.");
			
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR,e1);
			}
			//系统异常
			if(e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR,e);
		}
	}
}

